package com.saic.data.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;

public class ZookeeperUtil {

	public final String rootpath = "/hbase_oracle_map";
	public static final String zkaddress = "10.32.47.104:2181,10.32.47.105:2181,10.32.47.106:2181";// TODO
	public static final String zkNameSpace = "finance_platform";
	private CuratorFramework client = null;
	

	public ZookeeperUtil() throws Exception {
		RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 5);
		this.client = CuratorFrameworkFactory.builder()
				.connectString(zkaddress).retryPolicy(retryPolicy)
				.namespace(zkNameSpace).build();
		client.start();
	}

	public void add2ZK(String path,String msg) {
		try {
			client.create().creatingParentsIfNeeded()
					.withMode(CreateMode.PERSISTENT)
					.forPath(path, msg.getBytes());
		} catch (Exception e) {
			pm("add to ZK failed!"+ e.getMessage());
		}
		
	}

	public String getFromZK(String path) {
		String infos = null;
		try {
			infos = new String(client.getData().forPath(path));
		} catch (Exception e) {
			pm("get info from ZK:"+path+" failed!"+ e.getMessage());
		}
		return infos;
		
	}
	
	public Map<String, String> getProperties(String path) {
		Map<String, String> map =new HashMap<String, String>();
		List<String> list = new ArrayList<String>();
		try {
			list = client.getChildren().forPath(path);
			if (list.isEmpty()) return null;
			for(String key : list){
				map.put(key, getFromZK(path+"/"+key));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return map;
	}
	
	public void deleteChild(String path) throws Exception{
		List<String> list = new ArrayList<String>();
		list = client.getChildren().forPath(path);
		if (list.isEmpty()) client.delete().forPath(path);
		else{
			for(String child:list)
				deleteChild(path+"/"+child);
		}		
	}

	public void deleteNode(String path) throws Exception{
		List<String> list = new ArrayList<String>();
		list = client.getChildren().forPath(path);
		if (list.isEmpty()) client.delete().forPath(path);
		else{
			for(String child:list){
				deleteChild(path+"/"+child);
				client.delete().forPath(path);
				}
		}		
	}
	public void close() {
		if (null != client)
			client.close();
		client = null;
	}
	private void pm(String msg) {
		pm(this.getClass(), msg);
	}
	private void pm(Class<?> cls, String msg) {
		pm(cls.getName(), msg);
	}

	private void pm(String cls, String msg) {
		String data = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		System.out.println("["+data+"] Print by " + cls + "-->" + msg);
	}
}
